import flash.geom.Point;

class ScreenCoordsRadar extends ScreenCoordsBase {
	var TO_RADIANS:Int ;
	var range:Range;
	var angles:Float;
	var angle:Float;
	var radius:Float;
	
	public function new(top:Float, left:Float, right:Float, bottom:Float) {
		
		super(top, left, right, bottom);
		//
		// if the radar chart has labels this is going to
		// get updated so they fit onto the screen
		//
		this.TO_RADIANS = Math.PI / 180;
		this.radius = ( Math.min( this.width, this.height ) / 2.0 );
	}
	
	// axis range, from center to outer edge
	public function set_range( r:Range ): Void {
		this.range = r;
	}
	
	public function get_max():Float {
		return this.range.max;
	}
	
	// how many axis/spokes
	public function set_angles( a:Float ):Void {
		this.angles = a;
		this.angle = 360 / a;
	}
	
	public function get_angles():Float {
		return this.angles;
	}
	
	public function get_radius():Float {
		
		return this.radius;
	}
	
	public function reduce_radius():Void {
		this.radius--;
	}
	
	public function get_pos( angle:Float, radius:Float ): flash.geom.Point {
		
		// flash assumes 0 degrees is horizontal to the right
		var a:Int = (angle - 90) * TO_RADIANS;
		var r:Int = this.get_radius() * radius;
		
		var p:flash.geom.Point = new flash.geom.Point(
			r * Math.cos(a),
			r * Math.sin(a) );
			
		return p;
	}
	
	public override function get_get_x_from_pos_and_y_from_val( index:Float, y:Float, right_axis:Bool = false ):flash.geom.Point {
		
		// rotate
		var p:flash.geom.Point = this.get_pos( this.angle*index, y / this.range.count() );
		
		// translate
		p.x += this.get_center_x();
		p.y += this.get_center_y();
		
		return p;
	}
	
	public override function get_y_from_val( y:Float, right_axis:Bool = false ):Float {
		
		// rotate
		var p:flash.geom.Point = this.get_pos( 0, y / this.range.count() );
		
		// translate
		p.y += this.get_center_y();
		
		return p.y;
	}
}
